package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.BreedReport;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/15.
 */
@Repository
public class BreedReportDao extends MyBatisDao<BreedReport> {

    /**
     * 返回当前单据的编码的最大值
     *
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number) {
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"), number);
    }

    public Integer findStatusById(Long id) {
        return getSqlSession().selectOne(sqlId("findStatusById"), id);
    }

    public Paging<BreedReport> pagingBreedReport(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(this.sqlId("countAll"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<BreedReport> data = getSqlSession().selectList(this.sqlId("pagingAll"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 查询多个养殖场id的产地检疫申报单据
     *
     * @param offset   从多少行开始
     * @param limit    多少条
     * @param criteria 参数
     * @return Paging<BreedReport>
     */
    public Paging<BreedReport> pagingMulti(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(this.sqlId("countMulti"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<BreedReport> data = getSqlSession().selectList(this.sqlId("pagingMulti"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 查询多个养殖场id的产地检疫申报单据
     *
     * @param offset   从多少行开始
     * @param limit    多少条
     * @param criteria 参数
     * @return Paging<BreedReport>
     */
    public Paging<BreedReport> pagingMultiGF(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(this.sqlId("countMultiGF"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<BreedReport> data = getSqlSession().selectList(this.sqlId("pagingMultiGF"), criteria);
            return new Paging<>(total, data);
        }
    }

    public Paging<BreedReport> pagingCommon(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(this.sqlId("count2"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<BreedReport> data = getSqlSession().selectList(this.sqlId("paging2"), criteria);
            return new Paging<>(total, data);
        }
    }


    public List<Map> areaorder() {

        return getSqlSession().selectList(this.sqlId("areaorder"));
    }

    public List<Map> applyorder() {

        return getSqlSession().selectList(this.sqlId("applyorder"));
    }

    public Paging<BreedReport> cert(Integer offset, Integer limit, Map<String, Object> criteria) {
        Long total = (Long) getSqlSession().selectOne(this.sqlId("certcount"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<BreedReport> data = getSqlSession().selectList(this.sqlId("cert"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 溯源
     * @param criteria 参数
     * @return BreedReport
     */
    public BreedReport findByMarkNumber(Map<String , Object> criteria){
        return getSqlSession().selectOne(sqlId("findByMarkNumber"),criteria);
    }
}